﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using ChessGameV2.AI;
using ChessGameV2.Scenes;

namespace ChessGameV2
{
    public enum GameState { Start, Playing, Pause, Stop };
    class GameControl
    {
        #region properties 
        public int[] board = null;            //Trang thai ban co
        public static int TotalTime=0; // Tong thoi gian
        public static int defaultPlayerTime = 60000; // Bien Time

        SpriteBatch sp; // Bien engine
        Player player1; // Player1
        Player player2; // Player2
        Player player; // Player dang duoc chon.

        Moves listMove; // danh sach cac nuoc co the di chuyen

        internal GameState gameState;  //Trang thai game      
        PlayScene playScene; // DO HOA GAME
        #endregion

        public GameControl(SpriteBatch _sp)
        {
            this.sp = _sp;
            playScene = new PlayScene(this.sp);
            gameState = GameState.Start;
        }
        public void LoadContent(ContentManager Content) // Load Content
        {
            playScene.LoadContent(Content);
        }

        #region PHAN XU LI
        private void play(GameTime gameTime)
        {
            player.time -= gameTime.ElapsedGameTime.Milliseconds;

            //Cac trang thai ket thuc
            if (player.playerState == PlayerState.Win || player.playerState == PlayerState.Draw || player.playerState == PlayerState.Lost)
            {
                gameState = GameState.Stop;
                //player = null;
                return;
            }

            //Cac trang thai khi choi
            if (player.playerState == PlayerState.firstThinking)
            {
                player.time = defaultPlayerTime;
                listMove = new Moves(player.id, board);
                player.checkState(listMove, this.board);
            }
            else if (player.playerState == PlayerState.Thinking)
            {
                if (player.type == PlayerType.Human) // Voi Human
                {
                    player.ExecutebyHuman(this.board,this.listMove,playScene.getPieceCell());
                }
                else if (player.type == PlayerType.Computer) //Voi Computer
                {
                    player.Thinking(this.board, this.listMove);
                }
                else 
                {
                    player.move = player.listen();
                    player.ExecuteMove(board);
                }
            }
            else
            {
                nextPlayer();
            }
        }
        // Chuyen nguoi choi
        private void nextPlayer()
        {
            if (player.id == 1) player = player2;
            else player = player1;
            player.playerState = PlayerState.firstThinking;
        }
        // Trang thai bat dau game
        int slow = 21;
        public void gameStart(GameTime gameTime)
        {
            if (GameControl.TotalTime % 10 == 0 && slow < 99)
            {
                slow++;
                if (board[slow] != 0 && board[slow] != 99) Sounds.Click();                
                if (slow % 10 == 8)
                    slow += 2;
            }
            if (TotalTime > 5500)
            {
                Sounds.NewGame();
                gameState = GameState.Playing;
                TotalTime = 0;
                slow = 21;
            }
        }
        // Tro choi moi
        public void newGame(PlayerType p1, PlayerType p2)
        {
            
            int[] org = {
		                99,	99,	99,	99,	99,	99,	99,	99,	99,	99,
		                99,	99,	99,	99,	99,	99,	99,	99,	99,	99,
		                99,	124,22,	23,	25,	126,23,	22,	124,99,
		                99,	21,	21,	21,	21,	21,	21,	21,	21,	99,
		                99,	00,	00,	00,	00,	00,	00,	00,	00,	99,
		                99,	00,	00,	00,	00,	00,	00,	00,	00,	99,
		                99,	00,	00,	00,	00,	00,	00,	00,	00,	99,
		                99,	00,	00,	00,	00,	00,	00,	00,	00,	99,
		                99,	11,	11,	11,	11,	11,	11,	11,	11,	99,
		                99,	114,12,	13,	15,	116,13,	12,	114,99,
		                99,	99,	99,	99,	99,	99,	99,	99,	99,	99,
		                99,	99,	99,	99,	99,	99,	99,	99,	99,	99 };
            
                        
            board = new int[120];
            for (int i = 0; i < 120; i++)
                board[i] = org[i];

            player1 = new Player(1, Config.Config.p1);
            player2 = new Player(2, Config.Config.p2);
            player = player1;

            player.playerState = PlayerState.firstThinking;
            player.time = defaultPlayerTime;
            TotalTime = 0;

        }
        // UPDATE - Xu li trong game
        public void Update(GameTime gameTime)
        {
                TotalTime += gameTime.ElapsedGameTime.Milliseconds;
                if (gameState == GameState.Start)
                {
                    if (board == null)
                    {
                        newGame(Config.Config.p1, Config.Config.p1);
                        TotalTime = 0;
                    }
                    gameStart(gameTime);
                }
                else if (gameState == GameState.Playing)
                {
                    play(gameTime);
                }
                else if (gameState == GameState.Stop)
                {
                    //this.board = null;
                    // trang thai chua hoat dong
                }
                
        }
        #endregion
        #region DRAW
        public void Draw()
        {
                playScene.DrawBoard();
                if (gameState == GameState.Start)
                {
                    playScene.DrawSlowPieces(this.board,slow);
                    playScene.update();
                }
                else if (gameState == GameState.Playing)
                {
                    playScene.showTime(player.time);
                    if(player.type == PlayerType.Human) playScene.DrawSelectedCells(player.move, this.listMove);
                    //if (player.type == PlayerType.Computer && player.playerState == PlayerState.Thinking && player.test != null) playScene.DrawSelectedCell(player.test.Start);
                    playScene.DrawPieces(this.board);
                }
                else if (gameState == GameState.Stop)
                {
                    playScene.DrawPieces(this.board);
                    playScene.DrawState(player);
                }
        }
        #endregion
    }
}
